package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suray.commin.core.utils.StringUtils;
import com.suray.wcs.service.system.bo.NotificationBO;
import com.suray.wcs.service.system.entity.NotificationDB;
import com.suray.wcs.service.system.mapper.NotificationDBMapper;
import com.suray.wcs.service.system.service.INotificationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class NotificationServiceImpl implements INotificationService {
    @Resource
    private NotificationDBMapper notificationDBMapper;


    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<NotificationDB> selectNoticeList(NotificationDB notice) {
        LambdaQueryWrapper<NotificationDB> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(notice.getContent())) {
            queryWrapper.like(NotificationDB::getContent, notice.getContent());
        }
        if (notice.getNotificationType() != null) {
            queryWrapper.eq(NotificationDB::getNotificationType, notice.getNotificationType());
        }
        if (notice.getIsRead() != null) {
            queryWrapper.eq(NotificationDB::getIsRead, notice.getIsRead());
        }
        queryWrapper.orderByDesc(NotificationDB::getCreateTime);
        return notificationDBMapper.selectList(queryWrapper);
    }

    @Override
    public void readNotification(List<Long> notificationIds) {
        QueryWrapper<NotificationDB> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(NotificationDB::getIsRead, false);
        queryWrapper.lambda().in(true, NotificationDB::getNoticeId, notificationIds);
        List<NotificationDB> notificationDBS = notificationDBMapper.selectList(queryWrapper);
        if (notificationDBS != null) {
            for (NotificationDB notificationDB : notificationDBS) {
                notificationDB.setIsRead(true);
                notificationDB.setUpdateTime(new Date());
                notificationDBMapper.updateById(notificationDB);
            }
        }
    }

    @Override
    public void readAllNotification() {
        QueryWrapper<NotificationDB> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(NotificationDB::getIsRead, false);
        List<NotificationDB> notificationDBS = notificationDBMapper.selectList(queryWrapper);
        if (notificationDBS != null) {
            for (NotificationDB notificationDB : notificationDBS) {
                notificationDB.setIsRead(true);
                notificationDB.setUpdateTime(new Date());
                notificationDBMapper.updateById(notificationDB);
            }
        }
    }

    @Override
    public NotificationBO insertNotification(NotificationBO notificationBO) {
        NotificationDB notificationDB = new NotificationDB();
        BeanUtils.copyProperties(notificationBO, notificationDB);
        notificationDB.setCreateTime(new Date());
        notificationDBMapper.insert(notificationDB);
        return notificationBO;
    }

    @Override
    public void deleteNotification(Integer days) {
        notificationDBMapper.deleteNotification(days);
    }

    @Override
    public int selectNoticeCount(byte notificationType) {
        LambdaQueryWrapper<NotificationDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NotificationDB::getNotificationType, notificationType)
                .eq(NotificationDB::getIsRead, false);
        return notificationDBMapper.selectCount(queryWrapper);
    }
}
